package com.ftg.learn.demo;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 接收端
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/6/1 上午9:11
 */
public class Receiving {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("kjx");
        factory.setPassword("kjx");
        factory.setVirtualHost("/kjx");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        //监听队列里面是否有数据

        //有就是getmessage 没有waiting
        DeliverCallback deliverCallback = (x,y)->{
            String mess = new String(y.getBody(),"utf-8");

            //这段代码是否可以自己处理一下  service 添加数据库

            //可以收接到是短信 {telphone:13******,mess:'fdsafdsfdsfsdafdsfs'}
            //调用sms 直接发信息了

            System.out.println(mess+"||=>这是我的消费信息");
        };
        //消费者端
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    }

}
